package com.fh.modules.ums.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.ums.entity.UmsMemberReceiveAddress;
import com.fh.modules.ums.service.IUmsMemberReceiveAddressService;
import com.fhMall.request.QueryMemberAddressListRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

 /**
 * @Title: Controller
 * @Description: 会员收货地址
 * @author： code generator
 * @date：   2019-09-25
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("会员收货地址")
public class UmsMemberReceiveAddressController {
	@Autowired
	private IUmsMemberReceiveAddressService umsMemberReceiveAddressService;
	
	/**
	  * 分页列表查询
	 * @return
	 */
	@GetMapping(value = "/ums/umsMemberReceiveAddress/list")
	@ApiOperation("分页列表查询")
	@AutoLog(value="会员收货地址分页查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryPageList(Integer memberId) {
		LambdaQueryWrapper<UmsMemberReceiveAddress> query=new LambdaQueryWrapper();
		query.eq(UmsMemberReceiveAddress::getMemberId,memberId);
		query.eq(UmsMemberReceiveAddress::getDelFlag,0);
		query.orderByDesc(UmsMemberReceiveAddress::getIsDefault);
		return Result.ok(umsMemberReceiveAddressService.list(query));
	}
	
	/**
	  *   添加
	 * @return
	 */
	@PostMapping(value = "/ums/umsMemberReceiveAddress/add")
	@ApiOperation("添加")
	@AutoLog("添加会员收货地址")
	public Result add(@RequestBody UmsMemberReceiveAddress umsMemberReceiveAddress) {
		if(umsMemberReceiveAddress.getId()==null){
			umsMemberReceiveAddress.setDelFlag(CommonConstant.DEL_FLAG_0);
			umsMemberReceiveAddressService.save(umsMemberReceiveAddress);
		}else{
			umsMemberReceiveAddressService.updateById(umsMemberReceiveAddress);
		}

		return Result.ok("添加成功");
	}
	
	/**
	  *  编辑
	 * @param jsonObject
	 * @return
	 */
	@PutMapping(value = "/ums/umsMemberReceiveAddress/edit")
	@ApiOperation("修改")
	@AutoLog("修改会员收货地址")
	public Result edit(@RequestBody JSONObject jsonObject) {
		UmsMemberReceiveAddress umsMemberReceiveAddress = JSON.parseObject(jsonObject.toJSONString(), UmsMemberReceiveAddress.class);
		boolean ok = umsMemberReceiveAddressService.updateById(umsMemberReceiveAddress);
		return ok?Result.ok("修改成功"):Result.error("修改失败");
	}
	
	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/ums/umsMemberReceiveAddress/delete")
	@ApiOperation("通过id删除")
	@AutoLog("删除会员收货地址")
	public Result delete(@RequestParam(name="id",required=true) String id) {
		umsMemberReceiveAddressService.deleteEntityByDelFlag(id);
		return Result.ok("删除成功");
	}
	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/ums/umsMemberReceiveAddress/deleteBatch")
	@ApiOperation("批量删除")
	@AutoLog("批量删除会员收货地址")
	public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		umsMemberReceiveAddressService.deleteBatchEntityByDelFlag(ids);
		return Result.ok("批量删除成功");
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/ums/umsMemberReceiveAddress/queryById")
	@ApiOperation("通过id查询")
	@AutoLog(value="会员收货地址通过id查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryById(@RequestParam(name="id",required=true) Integer id) {
		UmsMemberReceiveAddress umsMemberReceiveAddress = umsMemberReceiveAddressService.getById(id);
		return Result.ok(umsMemberReceiveAddress);
	}


}
